package com.letoo.dragon.core.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.letoo.dragon.common.domain.Result;
import com.letoo.dragon.common.domain.ResultCode;
import com.letoo.dragon.common.utils.AliyunStsTokenUtils;
import com.letoo.dragon.core.service.AliyunStsTokenService;

/**
 * 激光推送服务接口实现
 * 
 * @author: dengjie
 */
@Service("aliyunStsTokenService")
public class AliyunStsTokenServiceImpl implements AliyunStsTokenService {

    private static final Logger LOG = LoggerFactory.getLogger(AliyunStsTokenServiceImpl.class);

    @Value("${sts.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${sts.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${sts.oss.roleArn}")
    private String roleArn;

    @Value("${sts.oss.roleSessionName}")
    private String roleSessionName;

    @Override
    public Result getOssUploadToken() {
        // 此处必须为 HTTPS
        ProtocolType protocolType = ProtocolType.HTTPS;
        AssumeRoleResponse response = AliyunStsTokenUtils.assumeRole(accessKeyId, accessKeySecret, roleArn,
                roleSessionName, protocolType);
        Result result = new Result(ResultCode.COMMON_SUCCESS, true);
        result.setProperty("expiration", response.getCredentials().getExpiration());
        result.setProperty("accessKeyId", response.getCredentials().getAccessKeyId());
        result.setProperty("accessKeySecret", response.getCredentials().getAccessKeySecret());
        result.setProperty("securityToken", response.getCredentials().getSecurityToken());
        return result;
    }

}
